using System;
using System.Collections.Generic;
using System.Linq;
using GameFrameworkLibraly.BaseObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameFrameworkLibraly.Controls
{
    public class MenuControl : Control
    {
        public Dictionary<Control, SubmenuControl> Submenues { get; private set; }
        public SubmenuControl SelectedSubmenu { get; private set; }

        /// <summary>
        /// Occures when menu is closed
        /// </summary>
        public event EventHandler MenuClosed;

        /// <summary>
        /// Creates a new instance of menu
        /// </summary>
        /// <param name="game">Current game</param>
        /// <param name="submenues">Dictionary of submenues, first parameter is a control that opens a submenu when it is released</param>
        /// <param name="backgroundTexture">Background texture for menu</param>
        public MenuControl(MyGame game, Dictionary<Control, SubmenuControl> submenues, Texture2D backgroundTexture):base(game)
        {
            Submenues = submenues;
            if (submenues.Count > 0)
            {
                SubmenuControl firstOrDefault = submenues.Where(i => i.Value.ParentMenu == null).FirstOrDefault().Value;
                SelectedSubmenu = firstOrDefault ?? submenues.First().Value;
            }
            foreach (var submenuControl in submenues)
            {
                submenuControl.Key.Released += ControlTapped;
                submenuControl.Value.SubMenuClosed += ValueSubMenuClosed;
            }
            BackgroundTexture = backgroundTexture;
        }

        
        #region Control
        /// <summary>
        /// Draws the control
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (!Visible)
                return;
            if (BackgroundTexture != null)
                spriteBatch.Draw(BackgroundTexture, DestinationRectangle, BackColorFilter);
            SelectedSubmenu.Draw(spriteBatch);
        }

        /// <summary>
        /// Updates state of the control
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (!Enabled)
                return;
            SelectedSubmenu.Update(gameTime);
        }

        /// <summary>
        /// Handles a touch and changes the state of this component if needed
        /// </summary>
        /// <param name="touchLocation">Touch Location</param>
        /// <returns>True, if touch was handled</returns>
        public override bool ProcessTouch(TouchLocation touchLocation)
        {
            if (!Enabled)
                return false;
            return SelectedSubmenu.ProcessTouch(touchLocation);
        }


        public override bool ProcessGesture(GestureSample gestureSample)
        {
            if (!Enabled)
                return false;
            return SelectedSubmenu.ProcessGesture(gestureSample);
        }
        #endregion

        /// <summary>
        /// Returns to parent menu if it's possible
        /// </summary>
        public void ReturnToParentMenu()
        {
            if (SelectedSubmenu.ParentMenu == null)
            {
                InvokeMenuClosed(EventArgs.Empty);
                return;
            }
            SelectedSubmenu = SelectedSubmenu.ParentMenu;
        }


        private void ControlTapped(object sender, EventArgs e)
        {
            SelectedSubmenu = Submenues[(Control) sender];
        }

        private void InvokeMenuClosed(EventArgs e)
        {
            EventHandler handler = MenuClosed;
            if (handler != null) handler(this, e);
        }

        private void ValueSubMenuClosed(object sender, EventArgs e)
        {
            ReturnToParentMenu();
        }

    }
}
